<?php


namespace App\Repositories;


use App\Models\Areas;
use App\Models\ExpressBlacklist as Model;
use LTools\Repositories\RepositoryAbstract;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class ExpressBlacklist extends RepositoryAbstract
{
    /**
     * ExpressBlacklist constructor.
     * @param Model $model
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Model $model)
    {
        $this->model = $model;
    }

    /**
     * @param $id
     * @return mixed
     * @author luffyzhao@vip.126.com
     */
    public function lists($id)
    {
        return $this->model->where('express_id', '=', $id)->get();
    }

    /**
     * @param array $attributes
     * @return bool|\Illuminate\Database\Eloquent\Model
     * @author luffyzhao@vip.126.com
     */
    public function create(array $attributes = [])
    {
        $count = Areas::query()->where('name', '=', $attributes['address'])->count();
        if (!$count) {
            throw new AccessDeniedHttpException('找不到这个省市区/县！');
        }
        return $this->model->newQuery()->firstOrCreate($attributes);
    }

    /**
     * @param $expressId
     * @param $id
     * @return bool|mixed|void
     * @author luffyzhao@vip.126.com
     */
    public function destroy($expressId, $id)
    {
        return $this->model->newQuery()->where('express_id', '=', $expressId)
            ->where('id', '=', $id)->delete();
    }
}